package com.atom.swipebacklib;

import android.graphics.Rect;
import android.support.v4.view.AccessibilityDelegateCompat;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.accessibility.AccessibilityEvent;

/**
 * ============================================================
 * project: MySwipBackLayout
 * package: com.atom.swipbacklib
 * fileDescribe:
 * user: admin
 * email: 1299854942@qq.com
 * createTime: 2017/7/27  11:45
 * modify:
 * version:: V1.0
 * ============================================================
 **/
public class SwipeBackAccessibilityDelegate extends AccessibilityDelegateCompat {

    private SwipeBackLayout mSwipeBackLayout;
    private final Rect mRect = new Rect();

    public SwipeBackAccessibilityDelegate(SwipeBackLayout mSwipeBackLayout){
       this.mSwipeBackLayout = mSwipeBackLayout;
    }

    @Override
    public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) {
        final AccessibilityNodeInfoCompat superNode = AccessibilityNodeInfoCompat.obtain(info);
        super.onInitializeAccessibilityNodeInfo(host, superNode);
        copyNodeInfoNoChildren(info, superNode);
        superNode.recycle();

        info.setClassName(SwipeBackLayout.class.getName());
        info.setSource(host);

        final ViewParent parent = ViewCompat.getParentForAccessibility(host);
        if (parent instanceof View) {
            info.setParent((View) parent);
        }

        // This is a best-approximation of addChildrenForAccessibility()
        // that accounts for filtering.
        final int childCount = mSwipeBackLayout.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = mSwipeBackLayout.getChildAt(i);
            if (!filter(child) && (child.getVisibility() == View.VISIBLE)) {
                // Force importance to "yes" since we can't read the value.
                ViewCompat.setImportantForAccessibility(
                        child, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
                info.addChild(child);
            }
        }
    }

    @Override
    public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
        super.onInitializeAccessibilityEvent(host, event);

        event.setClassName(SwipeBackLayout.class.getName());
    }

    @Override
    public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
                                                   AccessibilityEvent event) {
        if (!filter(child)) {
            return super.onRequestSendAccessibilityEvent(host, child, event);
        }
        return false;
    }


    public boolean filter(View child) {
        return mSwipeBackLayout.isDimmed(child);
    }


    /**
     *这应该是AccessibilityNodeInfoCompat，但不幸的是
     *似乎是使用底层API不容易克隆的几个元素。
     *将其隐藏在此处，因为它不是通用目的。
     */
    private void copyNodeInfoNoChildren(AccessibilityNodeInfoCompat dest,
                                        AccessibilityNodeInfoCompat src) {
        final Rect rect = mRect;

        src.getBoundsInParent(rect);
        dest.setBoundsInParent(rect);

        src.getBoundsInScreen(rect);
        dest.setBoundsInScreen(rect);

        dest.setVisibleToUser(src.isVisibleToUser());
        dest.setPackageName(src.getPackageName());
        dest.setClassName(src.getClassName());
        dest.setContentDescription(src.getContentDescription());

        dest.setEnabled(src.isEnabled());
        dest.setClickable(src.isClickable());
        dest.setFocusable(src.isFocusable());
        dest.setFocused(src.isFocused());
        dest.setAccessibilityFocused(src.isAccessibilityFocused());
        dest.setSelected(src.isSelected());
        dest.setLongClickable(src.isLongClickable());

        dest.addAction(src.getActions());

        dest.setMovementGranularities(src.getMovementGranularities());
    }
}
